/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.googlecode.datawander.shemareader;

import com.db4o.ObjectContainer;
import com.db4o.ext.Db4oDatabase;
import com.db4o.ext.ExtObjectContainer;
import com.db4o.ext.StoredClass;
import com.db4o.reflect.ReflectClass;
import com.db4o.reflect.Reflector;
import com.db4o.reflect.generic.GenericReflector;
import com.googlecode.datawander.connectors.Db4oUtil;
import com.googlecode.datawander.i18n.I18n;
import java.util.HashSet;
import java.util.Set;
import org.apache.log4j.Logger;

/**
 *
 * @author Marcin Stachniuk
 */
public class Db4oDatabaseReader {

    private static final Logger logger = Logger.getLogger(Db4oDatabaseReader.class);

    /**
     * String use to insert into querys before and after table and field names.
     */
    public static final String ESC_CHAR = "\"";

    private static final String [] IGNORE_CLASS_PREFIXS = new String [] {
            "java.lang.",
            "java.util.",
            "java.math.",
            "System.",
            "j4o.lang.AssemblyNameHint",
            "com.db4o.",
            // my added class
            "java.sql.",
            "Db4objects.Db4o.",
            "oracle.sql."
    };

    private Set<ReflectClass> userClasses;

    public void readDatabaseSchema() {
        System.out.println("readDatabaseSchema() "+Db4oUtil.getDatabaseFilename());

        //System.out.println("Reflector: " + Db4oUtil.getObjectContainer().ext().reflector().toString());

        userClasses = generateUserStoredClasses();
        logger.info(I18n.getString("readallclassessuces"));
    }

    private Set<ReflectClass> generateUserStoredClasses() {
        ObjectContainer db = Db4oUtil.getObjectContainer();
        ExtObjectContainer ext = db.ext();
        GenericReflector gr =  ext.reflector();
        //Db4oDatabase dat = db.ext().identity();

        StoredClass [] storedClass =
                //ext.knownClasses();
                // gr.knownClasses();
                ext.storedClasses();
        for(StoredClass rc : storedClass) {
            logger.trace("storedClass: " + rc.getName());
        }
        Set<ReflectClass> knowClasses = new HashSet<ReflectClass>();
        Reflector reflector = db.ext().reflector();
        for(StoredClass rc : storedClass) {
            boolean add = true;
            for (int i = 0; i < IGNORE_CLASS_PREFIXS.length; i++) {
                if(rc.getName().startsWith(IGNORE_CLASS_PREFIXS[i])) {
                    add = false;
                }
            }
            if(add) {
                knowClasses.add(reflector.forName(rc.getName()));
                String message = I18n.getString("readclassstoredindatabase");
                message = String.format(message, rc.getName());
                logger.debug(message);
            }
        }

        //String message

        return knowClasses;
    }

    public Set<ReflectClass> getUserClasses() {
        return userClasses;
    }

}
